package com.scalar.chiptrack.gpintegration.service;
import java.sql.Date;
import java.util.Calendar;
import java.util.ArrayList;


import com.scalar.ScalarException;
import com.scalar.chiptrack.gpintegration.LineItemDetail;
import com.scalar.chiptrack.gpintegration.LotDetail;
import com.scalar.chiptrack.gpintegration.OrderDetail;
import com.scalar.chiptrack.gpintegration.dao.GPIntegrationDAO;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.Mailer;
import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.users.notification.dao.NotificationDAO;
import com.scalar.chiptrack.commons.Constants;

public class GPInvoiceClient extends JobHandler
{
	public static void main(String[] args) throws Exception 
	{
        new GPInvoiceClient().execute();
	}

    public void execute() throws Exception
    {
        /*OrderDetail orderDetail = null;
        try
        {
            ServiceClient client = new ServiceClient();
            // create option object
            Options opts = new Options();
            //setting target EPR
            opts.setTo(new EndpointReference("http://64.3.133.250/OrderConnectorWS/OrderConnectorWS.asmx"));
            //Setting action ,and which can be found from the wsdl of the service
            opts.setAction("http://localhost/OrderConnector/AddOrder");
            //setting created option into service client
            client.setOptions(opts);

            orderDetail 	= GPIntegrationDAO.getShipmentInfo();

            if( orderDetail != null && orderDetail.getPDocumentNumber() != null && orderDetail.getPDocumentNumber().length() > 1 )
            {
                client.sendReceive( createAddOrderObject( orderDetail ) );

                // To add the line items
                opts.setTo(new EndpointReference("http://64.3.133.250/OrderConnectorWS/OrderConnectorWS.asmx"));
                opts.setAction("http://localhost/OrderConnector/AddOrderDetails");
                client.setOptions(opts);
                if( orderDetail.getLineItemList() != null && orderDetail.getLineItemList().size() > 0 )
                {
                    for( LineItemDetail lineItemDetail:orderDetail.getLineItemList() )
                    {
                        client.sendReceive( createAddOrderDetailsObject( lineItemDetail ) );
                    }
                }

                // To add the lot details
                opts.setTo(new EndpointReference("http://64.3.133.250/OrderConnectorWS/OrderConnectorWS.asmx"));
                //Setting action ,and which can be found from the wsdl of the service
                opts.setAction("http://localhost/OrderConnector/AddOrderDetailLots");
                //setting created option into service client
                client.setOptions(opts);
                if( orderDetail.getLineItemList() != null && orderDetail.getLineItemList().size() > 0 )
                {
                    for( LineItemDetail lineItemDetail:orderDetail.getLineItemList() )
                    {
                        for( LotDetail lotDetail:lineItemDetail.getLotDetailList() )
                        {
                            client.sendReceive( createAddOrderDetailLotsObject( lotDetail ) );
                        }
                    }
                }
                if ( orderDetail.getCustomerShippingId() > 0 )
                {
                    GPIntegrationDAO.updateInvoiceStatus(null, orderDetail.getCustomerShippingId() );
                }
            }
        }
        catch (ScalarException e)
        {
			e.printStackTrace();
            String fromEmail = ConfigManager.getScalarAdminEmail();
            ArrayList<String> emailList = NotificationDAO.loadEmailsByNotificationType(null, "GP Porting" );
            String[] emails = new String[emailList.size()];
            emailList.toArray(emails);
            String toEmails = StringUtils.concatStrings( emails, Mailer.EMAIL_SEPARATOR );
            String portingStatusMsg = null;

            portingStatusMsg = preparePortingStatusMsg(orderDetail);
            try
            {
                StringBuffer msg = new StringBuffer ( );
                msg.append ( "<HTML><HEAD><TITLE>GP Order Porting Status</TITLE></HEAD>" );
                msg.append ( "<BODY ALIGN='CENTER'><TABLE STYLE='FONT-SIZE: 8PT; COLOR: BLACK; FONT-FAMILY: VERDANA' BORDERCOLOR='#FFFFFF' CELLSPACING='1' CELLPADDING='3' BORDER=0 ALIGN='CENTER'>" );
                msg.append ( "<TR BGCOLOR='#CCCCFF'><TD COLSPAN='7' ALIGN='CENTER'><B>GP ORDER PORTING STATUS REPORT</B></TD></TR>" );
                msg.append ( "<TR BGCOLOR='#D3D3D6' ALIGN='CENTER'>" );
                msg.append ( "<TD><B>S.No</B></TD><TD><B>SALES ORDER #</B></TD><TD><B>CUSTOMER #</B></TD><TD><B>INVOICE#</B></TD>" +
                        "<TD><B>PRODUCT</B></TD><TD><B>Line Item#</B></TD><TD><B>LOT NUMBER</B></TD>" );
                msg.append ( "</TR>" );
                msg.append ( portingStatusMsg );
                msg.append ( "</TABLE></BODY></HTML>" );

                //System.out.println(msg.toString());
                //log(msg.toString());

                sendSalesOrdersPortingAlert( "GP Order porting status alert", msg.toString(), fromEmail, toEmails );
                System.out.println ( "GP Porting status mail sent successfully" );
            }
            catch ( ScalarException ee )
            {
                System.out.println ( "***WARNING*** Could not send Porting Status alert mail" );
                ee.printStackTrace();
            }

		}*/
    }
	
	/*public static OMElement createAddOrderDetailLotsObject( LotDetail lotDetail )
	{
		SOAPFactory fac 			= OMAbstractFactory.getSOAP12Factory();
        OMNamespace omNs 			= fac.createOMNamespace( "http://localhost/OrderConnector/", "s");
        OMElement addOrderDetailsLots 	= fac.createOMElement("AddOrderDetailLots", omNs );
		OMElement pDocumentNumber  	= fac.createOMElement("pDocumentNumber", omNs);
		OMElement pDocumentType  	= fac.createOMElement("pDocumentType", omNs);
		OMElement pLineItemSequence = fac.createOMElement("pLineItemSequence", omNs);
		OMElement pComponentSequence = fac.createOMElement("pComponentSequence", omNs);
		OMElement pLotSequence 		= fac.createOMElement("pLotSequence", omNs);
		OMElement pItemNumber  		= fac.createOMElement("pItemNumber", omNs);
		OMElement pLotNumber  		= fac.createOMElement("pLotNumber", omNs);
		OMElement pQTY  			= fac.createOMElement("pQTY", omNs);
		OMElement pLotAttribute1  	= fac.createOMElement("pLotAttribute1", omNs);
		OMElement pLotAttribute2  	= fac.createOMElement("pLotAttribute2", omNs);
		OMElement pLotAttribute3  	= fac.createOMElement("pLotAttribute3", omNs);
		OMElement pLotAttributeDate1 = fac.createOMElement("pLotAttributeDate1", omNs);
		OMElement pLotAttributeDate2 = fac.createOMElement("pLotAttributeDate2", omNs);
		OMElement pLotAttributeDate3 = fac.createOMElement("pLotAttributeDate3", omNs);
        
		pDocumentNumber.setText	( lotDetail.getPDocumentNumber() );
		pDocumentType.setText	( lotDetail.getPDocumentType() );
		pLineItemSequence.setText( lotDetail.getPLineItemSequence() );
		pItemNumber.setText		( ( lotDetail.getPItemNumber() != null && lotDetail.getPItemNumber().length() > 24 )?lotDetail.getPItemNumber().substring(0, 24): lotDetail.getPItemNumber() );
		pComponentSequence.setText( "0" );
		pLotSequence.setText	( lotDetail.getPLotSequence()+"" );
		pLotNumber.setText		( lotDetail.getPLotNumber() );
		pQTY.setText			( lotDetail.getPQTY()+"" );
		pLotAttribute1.setText	( "" );
		pLotAttribute2.setText	( "" );
		pLotAttribute3.setText	( "" );
		pLotAttributeDate1.setText( StringUtils.getSQLDate( "10/10/2017" )+"" );
		pLotAttributeDate2.setText( StringUtils.getSQLDate( "10/10/2017" )+"" );
		pLotAttributeDate3.setText( StringUtils.getSQLDate( "10/10/2017" )+"" );

		addOrderDetailsLots.addChild( pDocumentNumber );
		addOrderDetailsLots.addChild( pDocumentType );
		addOrderDetailsLots.addChild( pLineItemSequence );
		addOrderDetailsLots.addChild( pItemNumber );
		addOrderDetailsLots.addChild( pComponentSequence );
		addOrderDetailsLots.addChild( pLotSequence );
		addOrderDetailsLots.addChild( pLotNumber );
		addOrderDetailsLots.addChild( pQTY );
		addOrderDetailsLots.addChild( pLotAttribute1 );
		addOrderDetailsLots.addChild( pLotAttribute2 );
		addOrderDetailsLots.addChild( pLotAttribute3 );
		addOrderDetailsLots.addChild( pLotAttributeDate1 );
		addOrderDetailsLots.addChild( pLotAttributeDate2 );
		addOrderDetailsLots.addChild( pLotAttributeDate3 );
        
		return addOrderDetailsLots;
	}
	

	public static OMElement createAddOrderDetailsObject( LineItemDetail lineItemDetail ) 
	{
		SOAPFactory fac 			= OMAbstractFactory.getSOAP12Factory();
        OMNamespace omNs 			= fac.createOMNamespace( "http://localhost/OrderConnector/", "s");
        OMElement addOrderDetails 	= fac.createOMElement("AddOrderDetails", omNs );
		OMElement pDocumentNumber  	= fac.createOMElement("pDocumentNumber", omNs);
		OMElement pDocumentType  	= fac.createOMElement("pDocumentType", omNs);
		OMElement pLineItemSequence = fac.createOMElement("pLineItemSequence", omNs);
		OMElement pItemNumber  		= fac.createOMElement("pItemNumber", omNs);
		OMElement pItemDescription  = fac.createOMElement("pItemDescription", omNs);
		OMElement pQTY  			= fac.createOMElement("pQTY", omNs);
		OMElement pUnitPrice  		= fac.createOMElement("pUnitPrice", omNs);
		OMElement pMarkdownAmount  	= fac.createOMElement("pMarkdownAmount", omNs);
		OMElement pUnitCost  		= fac.createOMElement("pUnitCost", omNs);
		OMElement pStandardCost  	= fac.createOMElement("pStandardCost", omNs);
		OMElement pItemClassCode  	= fac.createOMElement("pItemClassCode", omNs);
		OMElement pRequestedShipDate  = fac.createOMElement("pRequestedShipDate", omNs);
        
		pDocumentNumber.setText( lineItemDetail.getPDocumentNumber() );
		pDocumentType.setText( lineItemDetail.getPDocumentType() );
		pLineItemSequence.setText( lineItemDetail.getPLineItemSequence() );
		pItemNumber.setText( ( lineItemDetail.getPItemNumber() != null && lineItemDetail.getPItemNumber().length() > 30 )?lineItemDetail.getPItemNumber().substring(0, 30): lineItemDetail.getPItemNumber() );
		pItemDescription.setText( ( lineItemDetail.getPItemDescription() != null && lineItemDetail.getPItemDescription().length() > 50 )? lineItemDetail.getPItemDescription().substring(0, 50): lineItemDetail.getPItemDescription() );
		pQTY.setText( lineItemDetail.getPQTY()+"" );
		pUnitPrice.setText( lineItemDetail.getPUnitPrice()+"" );
		pMarkdownAmount.setText( 0.0+"" );
		pUnitCost.setText( lineItemDetail.getPUnitCost()+"" );
		pStandardCost.setText( lineItemDetail.getPStandardCost()+"" );
		pItemClassCode.setText( lineItemDetail.getPItemClassCode() );
		pRequestedShipDate.setText( StringUtils.getSQLDate( lineItemDetail.getPRequestedShipDate() )+"" );

		addOrderDetails.addChild( pDocumentNumber );
		addOrderDetails.addChild( pDocumentType );
		addOrderDetails.addChild( pLineItemSequence );
		addOrderDetails.addChild( pItemNumber );
		addOrderDetails.addChild( pItemDescription );
		addOrderDetails.addChild( pQTY );
		addOrderDetails.addChild( pUnitPrice );
		addOrderDetails.addChild( pMarkdownAmount );
		addOrderDetails.addChild( pUnitCost );
		addOrderDetails.addChild( pStandardCost );
		addOrderDetails.addChild( pItemClassCode );
		addOrderDetails.addChild( pRequestedShipDate );
		
        
		return addOrderDetails;
	}

	public static OMElement createAddOrderObject( OrderDetail orderDetail ) 
	{
		SOAPFactory fac 	= OMAbstractFactory.getSOAP12Factory();
        OMNamespace omNs 	=   fac.createOMNamespace( "http://localhost/OrderConnector/", "s");

		OMElement addOrder 			= fac.createOMElement("AddOrder", omNs);
		OMElement pDocumentNumber  	= fac.createOMElement("pDocumentNumber", omNs);
		OMElement pDocumentType  	= fac.createOMElement("pDocumentType", omNs);
		OMElement pDocumentDate  	= fac.createOMElement("pDocumentDate", omNs);
		OMElement pOriginalNumber  	= fac.createOMElement("pOriginalNumber", omNs);
		OMElement pInvoiceDate  	= fac.createOMElement("pInvoiceDate", omNs);
		OMElement pCustomerPOID  	= fac.createOMElement("pCustomerPOID", omNs);
		OMElement pBatchID  		= fac.createOMElement("pBatchID", omNs);
		OMElement pIDCustomer  		= fac.createOMElement("pIDCustomer", omNs);
		OMElement pLocationCode  	= fac.createOMElement("pLocationCode", omNs);
		OMElement pCustomerClassID  = fac.createOMElement("pCustomerClassID", omNs);
		OMElement pBillToAddressID  = fac.createOMElement("pBillToAddressID", omNs);
		OMElement pBillToAddress1  	= fac.createOMElement("pBillToAddress1", omNs);
		OMElement pBillToAddress2  	= fac.createOMElement("pBillToAddress2", omNs);
		OMElement pBillToAddress3  	= fac.createOMElement("pBillToAddress3", omNs);
		OMElement pBillToCity  		= fac.createOMElement("pBillToCity", omNs);
		OMElement pBillToStateCode  = fac.createOMElement("pBillToStateCode", omNs);
		OMElement pBillToZip  		= fac.createOMElement("pBillToZip", omNs);
		OMElement pBillToCountryCode= fac.createOMElement("pBillToCountryCode", omNs);
		OMElement pBillToEmail  	= fac.createOMElement("pBillToEmail", omNs);
		OMElement pBillToName  		= fac.createOMElement("pBillToName", omNs);
		OMElement pBillToPhone  	= fac.createOMElement("pBillToPhone", omNs);
		OMElement pBillToFax  		= fac.createOMElement("pBillToFax", omNs);
		OMElement pContactPerson  	= fac.createOMElement("pContactPerson", omNs);
		OMElement pTaxAmount  		= fac.createOMElement("pTaxAmount", omNs);
		OMElement pFreightAmount  	= fac.createOMElement("pFreightAmount", omNs);
		OMElement pMiscellaneousAmount  		= fac.createOMElement("pMiscellaneousAmount", omNs);
		OMElement pDiscountAmount  	= fac.createOMElement("pDiscountAmount", omNs);
		OMElement pTotal  			= fac.createOMElement("pTotal", omNs);
		OMElement pShipMethodCode  	= fac.createOMElement("pShipMethodCode", omNs);
		OMElement pShipToAddressID  = fac.createOMElement("pShipToAddressID", omNs);
		OMElement pShipToName  		= fac.createOMElement("pShipToName", omNs);
		OMElement pShipToAddress1  	= fac.createOMElement("pShipToAddress1", omNs);
		OMElement pShipToAddress2  	= fac.createOMElement("pShipToAddress2", omNs);
		OMElement pShipToAddress3  	= fac.createOMElement("pShipToAddress3", omNs);
		OMElement pShipToCity  		= fac.createOMElement("pShipToCity", omNs);
		OMElement pShipToStateCode  = fac.createOMElement("pShipToStateCode", omNs);
		OMElement pShipToZip  		= fac.createOMElement("pShipToZip", omNs);
		OMElement pShipToCountryCode = fac.createOMElement("pShipToCountryCode", omNs);
		OMElement pShipToEmail  	= fac.createOMElement("pShipToEmail", omNs);
		OMElement pShipToPhone  	= fac.createOMElement("pShipToPhone", omNs);
		OMElement pShipToFax  		= fac.createOMElement("pShipToFax", omNs);
		OMElement pOrderConfirmationNumber  		= fac.createOMElement("pOrderConfirmationNumber", omNs);
		OMElement pCommentText  	= fac.createOMElement("pCommentText", omNs);
		OMElement pUserDefinedDate1 = fac.createOMElement("pUserDefinedDate1", omNs);
		OMElement pUserDefinedDate2 = fac.createOMElement("pUserDefinedDate2", omNs);
		OMElement pUserDefinedTable1	= fac.createOMElement("pUserDefinedTable1", omNs);
		OMElement pUserDefinedTable2	= fac.createOMElement("pUserDefinedTable2", omNs);
		OMElement pUserDefinedTable3	= fac.createOMElement("pUserDefinedTable3", omNs);
		OMElement pUserDefined1  		= fac.createOMElement("pUserDefined1", omNs);
		OMElement pUserDefined2  		= fac.createOMElement("pUserDefined2", omNs);
		OMElement pUserDefined3  		= fac.createOMElement("pUserDefined3", omNs);
		OMElement pUserDefined4  		= fac.createOMElement("pUserDefined4", omNs);
		OMElement pUserDefined5  		= fac.createOMElement("pUserDefined5", omNs);
		
		if( orderDetail != null )
		{
			pDocumentNumber.setText( orderDetail.getPDocumentNumber() );
			System.out.println("pDocumentNumber:" + orderDetail.getPDocumentNumber());
			pDocumentType.setText( orderDetail.getPDocumentType() );
			System.out.println("pDocumentType:" + orderDetail.getPDocumentType());
			pDocumentDate.setText( StringUtils.getSQLDate( orderDetail.getPDocumentDate(), "yyyy-MM-dd HH:mm:ss.SSS" )+"" );
			System.out.println("pDocumentDate:" + StringUtils.getSQLDate( orderDetail.getPDocumentDate(), "yyyy-MM-dd HH:mm:ss.SSS"  )+"");
			pOriginalNumber.setText( "" );
			pInvoiceDate.setText( StringUtils.getSQLDate( orderDetail.getPDocumentDate(), "yyyy-MM-dd HH:mm:ss.SSS" )+""  );
			System.out.println("pInvoiceDate:" + StringUtils.getSQLDate( orderDetail.getPDocumentDate(), "yyyy-MM-dd HH:mm:ss.SSS" )+"");
			pCustomerPOID.setText( ( orderDetail.getPCustomerPOID() != null && orderDetail.getPCustomerPOID().length() > 20 )?orderDetail.getPCustomerPOID().substring(0, 20): orderDetail.getPCustomerPOID() );
			System.out.println("pCustomerPOID:" + orderDetail.getPCustomerPOID());
			
			Calendar cal = Calendar.getInstance();
			cal.setTime( StringUtils.format(  orderDetail.getPDocumentDate(), "yyyy-MM-dd HH:mm:ss.SSS" ) );
			System.out.println( DateUtil.getCurrentMonth( cal )+""+DateUtil.getCurrentDay( cal ) + "" + DateUtil.getCurrentYear( cal ) );
			String batchId = orderDetail.getPIDCustomer().substring(0, 4)+ DateUtil.getCurrentMonth( cal )+""+DateUtil.getCurrentDay( cal ) + "" + DateUtil.getCurrentYear( cal );
			pBatchID.setText( batchId );
			System.out.println("pBatchID:" + batchId );
			pIDCustomer.setText( ( orderDetail.getPIDCustomer() != null && orderDetail.getPIDCustomer().length() > 15 )?orderDetail.getPIDCustomer().substring(0, 15): orderDetail.getPIDCustomer() );
			System.out.println("pIDCustomer:" + orderDetail.getPIDCustomer() );
			pLocationCode.setText( ( orderDetail.getPLocationCode() != null && orderDetail.getPLocationCode().length() > 10 )?orderDetail.getPLocationCode().substring(0, 10): orderDetail.getPLocationCode() );// to be done..."WAREHOUSE"
			System.out.println("pLocationCode:" + orderDetail.getPLocationCode() );
			pCustomerClassID.setText( "USA-ILMO-T1" ); //to be done...
			pBillToAddressID.setText( orderDetail.getPBillToAddressID() );
			System.out.println("pBillToAddressID:" + orderDetail.getPBillToAddressID() );
			pBillToName.setText( orderDetail.getPBillToName() );
			System.out.println("pBillToName:" + orderDetail.getPBillToName() );
			pBillToAddress1.setText( ( orderDetail.getPBillToAddress1() != null && orderDetail.getPBillToAddress1().length() > 60 )?orderDetail.getPBillToAddress1().substring(0, 60): orderDetail.getPBillToAddress1() );
			System.out.println("pBillToAddress1:" + orderDetail.getPBillToAddress1() );
			pBillToAddress2.setText( ( orderDetail.getPBillToAddress2() != null && orderDetail.getPBillToAddress2().length() > 60 )?orderDetail.getPBillToAddress2().substring(0, 60): orderDetail.getPBillToAddress2() );
			System.out.println("pBillToAddress2:" + orderDetail.getPBillToAddress2() );
			pBillToAddress3.setText( ( orderDetail.getPBillToAddress3() != null && orderDetail.getPBillToAddress3().length() > 60 )?orderDetail.getPBillToAddress3().substring(0, 60): orderDetail.getPBillToAddress3() );
			System.out.println("pBillToAddress3:" + orderDetail.getPBillToAddress3() );
			pBillToCity.setText( ( orderDetail.getPBillToCity() != null && orderDetail.getPBillToCity().length() > 35 )?orderDetail.getPBillToCity().substring(0, 35): orderDetail.getPBillToCity());
			System.out.println("pBillToCity:" + orderDetail.getPBillToCity() );
			pBillToStateCode.setText( ( orderDetail.getPBillToStateCode() != null && orderDetail.getPBillToStateCode().length() > 30 )?orderDetail.getPBillToStateCode().substring(0, 30 ): orderDetail.getPBillToStateCode() );
			System.out.println("pBillToStateCode:" + orderDetail.getPBillToStateCode() );
			pBillToZip.setText( ( orderDetail.getPBillToZip() != null && orderDetail.getPBillToZip().length() > 30 )?orderDetail.getPBillToZip().substring(0, 30 ): orderDetail.getPBillToZip() );
			System.out.println("pBillToZip:" + orderDetail.getPBillToZip() );
			pBillToCountryCode.setText( ( orderDetail.getPBillToCountryCode() != null && orderDetail.getPBillToCountryCode().length() > 60 )?orderDetail.getPBillToCountryCode().substring(0, 60 ): orderDetail.getPBillToCountryCode() );
			System.out.println("pBillToCountryCode:" + orderDetail.getPBillToCountryCode() );
			pBillToEmail.setText( ( orderDetail.getPBillToEmail() != null && orderDetail.getPBillToEmail().length() > 100 )?orderDetail.getPBillToEmail().substring(0, 100 ): orderDetail.getPBillToEmail() );
			System.out.println("pBillToEmail:" + orderDetail.getPBillToEmail() );
			pBillToPhone.setText( ( orderDetail.getPBillToPhone() != null && orderDetail.getPBillToPhone().length() > 21 )?orderDetail.getPBillToPhone().substring(0, 21 ): orderDetail.getPBillToPhone() );
			System.out.println("pBillToPhone:" + orderDetail.getPBillToPhone() );
			pBillToFax.setText( ( orderDetail.getPShipToFax() != null && orderDetail.getPBillToFax().length() > 21 )?orderDetail.getPBillToFax().substring(0, 21 ): orderDetail.getPBillToFax() );
			System.out.println("pBillToFax:" + orderDetail.getPShipToFax() );
			pContactPerson.setText( orderDetail.getPContactPerson() );
			System.out.println("pContactPerson:" + orderDetail.getPContactPerson() );
			pTaxAmount.setText( "0.00" );
			pFreightAmount.setText( orderDetail.getPFreightAmount()+"" );
			pMiscellaneousAmount.setText( "0.0" );
			pDiscountAmount.setText( "0.0" );
			pTotal.setText( orderDetail.getPTotal()+"" );
			System.out.println("pTotal:" + orderDetail.getPTotal() );
			pShipMethodCode.setText( orderDetail.getPShipMethodCode() );
			System.out.println("pShipMethodCode:" + orderDetail.getPShipMethodCode() );
			pShipToAddressID.setText( orderDetail.getPShipToAddressID() );
			System.out.println("pShipToAddressID:" + orderDetail.getPShipToAddressID() );
			pShipToName.setText( orderDetail.getPShipToName() );
			System.out.println("pShipToName:" + orderDetail.getPShipToName() );
			pShipToAddress1.setText( ( orderDetail.getPShipToAddress1() != null && orderDetail.getPShipToAddress1().length() > 60 )?orderDetail.getPShipToAddress1().substring(0, 60): orderDetail.getPShipToAddress1() );
			System.out.println("pShipToAddress1:" + orderDetail.getPShipToAddress1() );
			pShipToAddress2.setText( ( orderDetail.getPShipToAddress2() != null && orderDetail.getPShipToAddress2().length() > 60 )?orderDetail.getPShipToAddress2().substring(0, 60): orderDetail.getPShipToAddress2() );
			System.out.println("pShipToAddress2:" + orderDetail.getPShipToAddress2() );
			pShipToAddress3.setText( ( orderDetail.getPShipToAddress3() != null && orderDetail.getPShipToAddress3().length() > 60 )?orderDetail.getPShipToAddress3().substring(0, 60): orderDetail.getPShipToAddress3() );
			System.out.println("pShipToAddress3:" + orderDetail.getPShipToAddress3() );
			pShipToCity.setText( ( orderDetail.getPShipToCity() != null && orderDetail.getPShipToCity().length() > 35 )?orderDetail.getPShipToCity().substring(0, 35): orderDetail.getPShipToCity() );
			System.out.println("pShipToCity:" + orderDetail.getPShipToCity() );
			pShipToStateCode.setText( ( orderDetail.getPShipToStateCode() != null && orderDetail.getPShipToStateCode().length() > 30 )?orderDetail.getPShipToStateCode().substring(0, 30 ): orderDetail.getPShipToStateCode() );
			System.out.println("pShipToStateCode:" + orderDetail.getPShipToStateCode() );
			pShipToZip.setText( ( orderDetail.getPShipToZip() != null && orderDetail.getPShipToZip().length() > 30 )?orderDetail.getPShipToZip().substring(0, 30 ): orderDetail.getPShipToZip() );
			System.out.println("pShipToZip:" + orderDetail.getPShipToZip() );
			pShipToCountryCode.setText( ( orderDetail.getPShipToCountryCode() != null && orderDetail.getPShipToCountryCode().length() > 60 )?orderDetail.getPShipToCountryCode().substring(0, 60 ): orderDetail.getPShipToCountryCode() );
			System.out.println("pShipToCountryCode:" + orderDetail.getPShipToCountryCode() );
			pShipToEmail.setText( ( orderDetail.getPShipToEmail() != null && orderDetail.getPShipToEmail().length() > 100 )?orderDetail.getPShipToEmail().substring(0, 100 ): orderDetail.getPShipToEmail() );
			System.out.println("pShipToEmail:" + orderDetail.getPShipToEmail() );
			pShipToPhone.setText( ( orderDetail.getPShipToPhone() != null && orderDetail.getPShipToPhone().length() > 21 )?orderDetail.getPShipToPhone().substring(0, 21 ): orderDetail.getPShipToPhone() );
			System.out.println("pShipToPhone:" + orderDetail.getPShipToPhone() );
			pShipToFax.setText( ( orderDetail.getPShipToFax() != null && orderDetail.getPShipToFax().length() > 21 )?orderDetail.getPShipToFax().substring(0, 21 ): orderDetail.getPShipToFax() );
			System.out.println("pShipToFax:" + orderDetail.getPShipToFax() );
			pOrderConfirmationNumber.setText( orderDetail.getPOrderConfirmationNumber() );
			System.out.println("pOrderConfirmationNumber:" + orderDetail.getPOrderConfirmationNumber() );
			pCommentText.setText( ( orderDetail.getPCommentText() != null && orderDetail.getPCommentText().length() > 150 )?orderDetail.getPCommentText().substring(0, 150 ): orderDetail.getPCommentText() );
			System.out.println("pCommentText:" + orderDetail.getPCommentText() );
			pUserDefinedDate1.setText( "" );// to be done..
			pUserDefinedDate2.setText( "" );// to be done..
			pUserDefinedTable1.setText( ( orderDetail.getPUserDefinedTable1() != null && orderDetail.getPUserDefinedTable1().length() > 20 )?orderDetail.getPUserDefinedTable1().substring(0, 20 ): orderDetail.getPUserDefinedTable1() );// to be done..
			pUserDefinedTable2.setText( "" );// to be done..
			pUserDefinedTable3.setText( "" );// to be done..
			pUserDefined1.setText( ( orderDetail.getPUserDefined1() != null && orderDetail.getPUserDefined1().length() > 20 )?orderDetail.getPUserDefined1().substring(0, 20 ): orderDetail.getPUserDefined1() );
			System.out.println("pUserDefined1:" + orderDetail.getPUserDefined1() );
			if ( orderDetail.getPUserDefined2() != null && orderDetail.getPUserDefined2().length() > 1 )
			{
				orderDetail.setPUserDefined2(  orderDetail.getPUserDefined2().replaceAll( ";", " ") );
			}
			pUserDefined2.setText( ( orderDetail.getPUserDefined2() != null && orderDetail.getPUserDefined2().length() > 20 )?orderDetail.getPUserDefined2().substring(0, 20 ): orderDetail.getPUserDefined2() );
			pUserDefined3.setText( ( orderDetail.getPUserDefined3() != null && orderDetail.getPUserDefined3().length() > 20 )?orderDetail.getPUserDefined3().substring(0, 20 ): orderDetail.getPUserDefined3() );
			pUserDefined4.setText( ( orderDetail.getPUserDefined4() != null && orderDetail.getPUserDefined4().length() > 20 )?orderDetail.getPUserDefined4().substring(0, 20 ): orderDetail.getPUserDefined4() );// to be done..
			pUserDefined5.setText( orderDetail.getPUserDefined5() );// to be done..
			
			addOrder.addChild( pDocumentNumber );
			addOrder.addChild( pDocumentType );
			addOrder.addChild( pDocumentDate );
			addOrder.addChild( pOriginalNumber );
			addOrder.addChild( pInvoiceDate );
			addOrder.addChild( pCustomerPOID );
			addOrder.addChild( pBatchID );
			addOrder.addChild( pIDCustomer );
			addOrder.addChild( pLocationCode );
			addOrder.addChild( pCustomerClassID );
			addOrder.addChild( pBillToAddressID );
			addOrder.addChild( pBillToAddress1 );
			addOrder.addChild( pBillToAddress2 );
			addOrder.addChild( pBillToAddress3 );
			addOrder.addChild( pBillToCity );
			addOrder.addChild( pBillToStateCode );
			addOrder.addChild( pBillToZip );
			addOrder.addChild( pBillToCountryCode );
			addOrder.addChild( pBillToEmail );
			addOrder.addChild( pBillToName );
			addOrder.addChild( pBillToPhone );
			addOrder.addChild( pBillToFax );
			addOrder.addChild( pContactPerson );
			addOrder.addChild( pTaxAmount );
			addOrder.addChild( pFreightAmount );
			addOrder.addChild( pMiscellaneousAmount );
			addOrder.addChild( pDiscountAmount );
			addOrder.addChild( pTotal );
			addOrder.addChild( pShipMethodCode );
			addOrder.addChild( pShipToAddressID );
			addOrder.addChild( pShipToName );
			addOrder.addChild( pShipToAddress1 );
			addOrder.addChild( pShipToAddress2 );
			addOrder.addChild( pShipToAddress3 );
			addOrder.addChild( pShipToCity );
			addOrder.addChild( pShipToStateCode );
			addOrder.addChild( pShipToZip );
			addOrder.addChild( pShipToCountryCode );
			addOrder.addChild( pShipToEmail );
			addOrder.addChild( pShipToPhone );
			addOrder.addChild( pShipToFax );
			addOrder.addChild( pOrderConfirmationNumber );
			addOrder.addChild( pCommentText );
			addOrder.addChild( pUserDefinedDate1 );
			addOrder.addChild( pUserDefinedDate2 );
			addOrder.addChild( pUserDefinedTable1 );
			addOrder.addChild( pUserDefinedTable2 );
			addOrder.addChild( pUserDefinedTable3 );
			addOrder.addChild( pUserDefined1 );
			addOrder.addChild( pUserDefined2 );
			addOrder.addChild( pUserDefined3 );
			addOrder.addChild( pUserDefined4 );
			addOrder.addChild( pUserDefined5 );
			                                                  
		}
		return addOrder;
	}

    private String preparePortingStatusMsg (OrderDetail orderDetail  )
    {
        StringBuffer statusMsg  = new StringBuffer();
        int count               = 0;
         for( LineItemDetail lineItemDetail:orderDetail.getLineItemList() )
         {
            for( LotDetail lotDetail:lineItemDetail.getLotDetailList() )
            {
                statusMsg.append ( "<TR BGCOLOR='#E6E6E6' ALIGN='CENTER'>" );
                statusMsg.append ( "<TD ALIGN='LEFT'>" ).append ( ++count ).append ( "</TD>" );
                statusMsg.append ( "<TD ALIGN='LEFT'>" ).append ( orderDetail.getPUserDefined4() ).append ( "</TD>" ); //SALES ORDER NUMBER
                statusMsg.append ( "<TD ALIGN='LEFT'>" ).append ( orderDetail.getPIDCustomer() ).append ( "</TD>" );  // customer#
                statusMsg.append ( "<TD ALIGN='LEFT'>" ).append ( orderDetail.getPDocumentNumber()  ).append ( "</TD>" );        //Invoice
                statusMsg.append ( "<TD ALIGN='CENTER'>" ).append ( lineItemDetail.getPItemNumber() ).append ( "</TD>" ); //part number
                statusMsg.append ( "<TD ALIGN='CENTER'>" ).append ( lineItemDetail.getPLineItemSequence() ).append ( "</TD>" );
                statusMsg.append ( "<TD ALIGN='CENTER'>" ).append ( lotDetail.getPLotNumber() ).append ( "</TD>" );    //Lot number
                statusMsg.append ( "</TR>");
            }
         }

        return statusMsg.toString();
    }

    private void sendSalesOrdersPortingAlert ( String subject, String message, String fromEmail, String toEmails ) throws Exception
    {

        //System.out.println ( "Sending alerts to: " + toEmails );
        try
        {
            Mailer.send ( fromEmail, toEmails, null, null, subject, message, null, Mailer.CONTENT_TYPE_HTML, false );
        }
        catch( Exception e )
        {
            throw new ScalarException ( "Could not send email notifications", e );
        }
    }*/
}
